﻿using KhoaLuan.Areas.Admin.ActionFilters;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KhoaLuan.Areas.Admin.Controllers
{
    [Security]
    public class ModelController : Controller
    {
        //
        // GET: /Admin/Model/
        LuanVanEntities db = new LuanVanEntities();
        public ActionResult Index()
        {
            var model = new Module();
            ViewBag.Module = db.Modules
               ;
            return View(model);
        }
        public ActionResult Insert(Module model)
        {
            var f = Request.Files["Upphoto"];
            if (!String.IsNullOrEmpty(f.FileName))
            {
                f.SaveAs(Server.MapPath("~/images/module/" + f.FileName));
                model.Image = f.FileName;
            }
            try
            {
                db.Modules.Add(model);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                ViewBag.Module = db.Modules;
                return View("Index", model);
            }
        }
        public ActionResult Update(Module model)
        {
            var f = Request.Files["UpPhoto"];
            if (!String.IsNullOrEmpty(f.FileName))
            {
                f.SaveAs(Server.MapPath("~/images/module/" + f.FileName));
                model.Image = f.FileName;

            }
            try
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                ModelState.AddModelError("", "Cập nhật Thành Công !");

            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);

            }
            ViewBag.Module = db.Modules;
            return View("Index", model);
        }
        public ActionResult Delete(int Id)
        {
            var model = db.Modules.Find(Id);
            db.Modules.Remove(model);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult Edit(int Id)
        {
            var model = db.Modules.Find(Id);
            ViewBag.Module = db.Modules;
            return View("Index", model);
        }

	}
}